<?php 
	class Test_home_works extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	function get_home_work_list($limit = 0, $offset = 10, $conditions = array()){
    		$where = " WHERE 1=1";
    		if(!empty($conditions['subject_id'])){
                $where .= " AND tv.subject_id = '{$conditions['subject_id']}'";
            }
            if(!empty($conditions['home_work_name'])){
            	$where .= ' AND tv.name like "%'.$conditions['home_work_name'].'%"';
            }
    		$sql = "SELECT SQL_CALC_FOUND_ROWS tv.*, t.teacher_name, s.title FROM {$this->db->dbprefix}tk_test_home_works AS tv ";
    		$sql .= "LEFT JOIN {$this->db->dbprefix}b_subject AS s ON(s.id=tv.subject_id) ";
    		$sql .= "LEFT JOIN {$this->db->dbprefix}o2o_teachers AS t ON(t.id=tv.teacher_id) ";
    		$sql .= "$where ORDER BY id DESC LIMIT $limit, $offset ";
    		$result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
    	}
    }